package com.hdoit.apollo.web.cargo.apis;

import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.apollo.logical.cargo.model.Picture;
import com.hdoit.apollo.logical.cargo.service.IPictureService;
import com.tennetcn.authority.component.webapi.BaseApi;
import com.tennetcn.authority.component.webapi.ResponseModel;
import com.tennetcn.core.common.enums.pub.YesOrNoInteger;
import com.tennetcn.core.common.utils.CommonUtils;
import com.tennetcn.core.common.utils.DateUtils;
import com.tennetcn.core.common.utils.JsonUtils;
import com.tennetcn.data.enums.ModelStatus;
import com.tennetcn.authority.component.webapi.ResponseStatus;
import io.swagger.annotations.Api;

@Controller
@RequestMapping(value = "/api/v1/picture/",method = { RequestMethod.GET,RequestMethod.POST},produces = "application/json;charset=UTF-8")
@CrossOrigin(origins = "*", maxAge = 3600)
@Api(value="/api/v1/picture")
public class PictureApi extends BaseApi{

	@Autowired
	private IPictureService pictureService;
	
	private final String savePictureApi="savePicture";
	
    @RequestMapping(value = savePictureApi)
    @ApiOperation(notes = savePictureApi, httpMethod = "POST", value = "保存图片信息检查信息")
    public @ResponseBody String savePicture(Picture pictureApi){
    	List<Picture> pictures = new ArrayList<Picture>();
    	@SuppressWarnings("unchecked")
		List<String> fileIds = (List<String>) JsonUtils.readValues(pictureApi.getFileId(), List.class, String.class);
    	System.err.println(fileIds);
    	System.err.println(pictureApi.getCargoId());
    	for(String fileId:fileIds){
    		Picture picture = new Picture();
    		picture.setPictureId(CommonUtils.getKeyId());
    		picture.setFileId(fileId);
    		picture.setCargoId(pictureApi.getCargoId());
    		picture.setMark(pictureApi.getMark());
    		
    		picture.setDeleteMark(YesOrNoInteger.no);
    		picture.setModelStatus(ModelStatus.add);
    		picture.setCreateDate(DateUtils.getCurrent());
    		picture.setCreateUserId(getLoginId());
    		picture.setCreateUserName(getLoginName());
    		pictures.add(picture);
    	}

    	ResponseModel rm=new ResponseModel();
    	if(pictureService.deleteByCargoId(pictureApi.getCargoId())) {
        	if(!pictureService.applyChanges(pictures)){
        		rm.setStatus(ResponseStatus.dataError);
        	}
    	}
    	return rm.toJson();
    }
}
